package mercado.aberto;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author monitor
 */
import java.sql.*;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;

public class EmpresaConvDAO extends DAO {
    
    public List<EmpresaConv> ListarTodos() throws Exception {

        Conectar();
        String SQL = "SELECT * FROM empconv_rest order by codigo";
        pstmt = con.prepareStatement(SQL);
        rs = pstmt.executeQuery();
        EmpresaConv ec = null;
        List<EmpresaConv> ListaEmpConv = new ArrayList<EmpresaConv>();
        while (rs.next()) {
            ec = new EmpresaConv();
            ec.setCodigo(rs.getString("codigo"));
            ec.setNome(rs.getString("nome"));
            ec.setEndereco(rs.getString("endereco"));
            ec.setStatus(rs.getString("status"));
            ListaEmpConv.add(ec);
        }
        Desconectar();
        return ListaEmpConv;
    }

    public EmpresaConv busca(String busca) throws Exception {
        Conectar();
        String SQL = "SELECT * FROM empconv_rest WHERE  codigo=? or nome=?";
        pstmt = con.prepareStatement(SQL);
        pstmt.setString(1, busca);
        pstmt.setString(2, busca);
        rs = pstmt.executeQuery();
        EmpresaConv ec = null;
        if (rs.next()) {
            ec = new EmpresaConv();
            ec.setCodigo(rs.getString("codigo"));
            ec.setNome(rs.getString("nome"));
            ec.setEndereco(rs.getString("endereco"));
            ec.setStatus(rs.getString("status"));
            
        }
        Desconectar();
        return ec;
    }
    
   public boolean cadastroEmpConv(EmpresaConv ec) {
        try {
            Conectar();
            //String SQL = "INSERT INTO produto_rest (nome, status, tipo) VALUES('"+p.getNome()+"','" + p.getStatus() +"','"+p.getTipo()+"')";
            String SQL = "INSERT INTO empconv_rest (nome, endereco, status) VALUES('"+ec.getNome()+"','" +ec.getEndereco()+"','"+ec.getStatus()+"')";
            Statement s = con.createStatement();
            s.executeUpdate(SQL);
            
            //System.out.println("ola");
            
            Desconectar();
            return true;
        } catch (Exception ex) {
            System.out.println("Erro " + ex);
            return false;
        }

    }
   
   public void remEmpConv (String nomEmp) throws Exception
    {
        try {
        Conectar();
        String SQL = ("delete from empconv_rest where nome=?");
        pstmt = con.prepareStatement(SQL);
        pstmt.setString(1, nomEmp);
        pstmt.executeUpdate();
        Desconectar();
        } catch (SQLException ex) {
           JOptionPane.showMessageDialog(null,""+ex,"Erro na Remoção do Produto", JOptionPane.ERROR_MESSAGE); 
        }
    }
   
   public boolean alterarEmpConv(EmpresaConv ec) {
        try {
            Conectar();
            String SQL = "update empconv_rest set nome = '"+ec.getNome()+"', endereco = '"+ec.getEndereco()+"' , status = '"+ec.getStatus()+"'";
            Statement s = con.createStatement();
            s.executeUpdate(SQL);
            //System.out.println("ola");
            Desconectar();
            return true;
        } catch (Exception ex) {
            System.out.println("Erro " + ex);
            return false;
        }

    }
    
}
